<?php
class Rate extends AppModel {

	var $name = 'Rate';
    var $belongsTo = array('Post'=>array(
        'className'=>'Post',
        'counterCache'=>true,
    ));

	/**
	 * Method used to update Rate
	 *
	 * @access public
	 * @param integer $photoId Id of photo
	 * @param string $visitorId Id of visitor
	 * @param integer $Rate Rate of photo
	 * @return boolean If Rate updated then return true else return false
	 */
	function updateRate($post_id,$ip) {
            $this->create();
            $this->data['Rate']['post_id']=$post_id;
            $this->data['Rate']['ip']=$ip;
            $this->data['Rate']['created']=date('Y-m-d H:i:s');
            if($this->save($this->data)){
                return 1;
            }else{
                return 2;
            }
     }

    /**
     * Checking rate history on 5 minutes and IP
     */
	function checkRate($post_id,$ip) {
        $result = $this->find('first',array(
                    'fields' => 'Rate.rate_id',
				    'conditions' => array('AND'=>array(
                        'Rate.post_id' =>$post_id,
                        'Rate.ip' => $ip,
                        "Rate.created >" => date('Y-m-d H:i:s', strtotime("-90 minute")))
				)
          ));

        return $result;
    }

}
?>